// pages/month_results/month_results.js
var util = require('../../utils/util');
var App = getApp();

function jsonSort(jsonObj) {
    let arr = [];
    for (var key in jsonObj) {
        arr.push(key)
    }
    arr.sort();
    let str = '';
    for (var i in arr) {
        str += arr[i] + "=" + jsonObj[arr[i]] + "&"
    }
    return str.substr(0, str.length - 1)
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        prev_time:'',
        array:[],
        member_list:[],
        membership_id:'',
        index:0,
        list:[],
        all_price:0,
        level:'',
        member_shipName:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var This = this;
        var time = util.formatTime(new Date());
        var date_value = time.slice(0, 4) + "-" + time.slice(5, 7)
        This.setData({
            prev_time: date_value,
            level:wx.getStorageSync('level')
        })

        This.member_list();
    },
    member_list() {
        var This = this;
        App._get('coach/member/get_coach_list', {
            openid: wx.getStorageSync('openid'),	
            store_id: wx.getStorageSync('store_id'),	
            phone: wx.getStorageSync('phone'),
            // openid: 1,
            // store_id: 53,
            // phone: 15612340001,
        }, function (res) { 
            if(res.status==1){
                var arrays = []
                for (var i = 0; i < res.coach_list.length;i++){
                    arrays.push(res.coach_list[i].coach_name)
                }
                This.setData({
                    member_list: arrays,
                    array: res.coach_list,
                    
                })
                if(arrays.length>0&&This.data.level == 2){
                    for (var index in res.coach_list){
                        if (res.coach_list[index].coach_name == '自己'){
                            This.setData({
                                membership_id: res.coach_list[index].id,
                                index: index
                            })
                        }
                    }  
                }else {
                    This.setData({
                        membership_id: res.coach_list[0].id,
                        index: 0
                    })
                }
                This.loading();
            }else{
                wx.showToast({
                    title: res.error,
                    image: '../../image/icon_danger.png',
                    duration: 2000
                })
            }
        })
    },
    loading(){
        var This = this;
        App._get('coach/member/get_coach_achievement',{
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            phone: wx.getStorageSync('phone'),
            // openid: 1,
            // store_id: 53,
            // phone: 15612340001,
            date_str: This.data.prev_time,
            coach_id:This.data.membership_id
        },function(res){
            if(res.status == 1){
                This.setData({
                    list: res.achievement_data,
                    all_price: res.achievement_money
                })
            }else{
                wx.showToast({
                    title: res.error,
                    image: '../../image/icon_danger.png',
                    duration: 2000
                })
            }
        })
    },
    bindDatesChange(e){
        var This = this;
        This.setData({
            list: [],
            prev_time:e.detail.value
        })
        This.loading();
    },
    bindChanges(e){
        var This = this;
        This.setData({
            list:[],
            index:e.detail.value,
            membership_id: This.data.array[e.detail.value].id,
            member_shipName: This.data.array[e.detail.value].coach_name
        })
        This.loading();
    },
    detail(e){
        var This = this;
        console.log(e)
        console.log()
        if (This.data.membership_id == 0){
            wx.navigateTo({
                url: '/pages/month_results_detail/month_results_detail?id=' + This.data.membership_id + '&time=' + e.currentTarget.dataset.time,
            })
        }else {
            wx.navigateTo({
                url: '/pages/month_results_detail_detail/month_results_detail_detail?id=' + This.data.membership_id + '&time=' + e.currentTarget.dataset.time + "&name=" + This.data.member_shipName + '&img=' + e.currentTarget.dataset.img,
            })
        }
        // if (This.data.membership_id == ''){
        //     wx.navigateTo({
        //         url: '/pages/month_results_detail/month_results_detail?id=' + '' + '&time=' + e.currentTarget.dataset.time,
        //     })
        // }else{
           
        // }
        
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})